package com.qf.dao.Impl;

import com.qf.dao.CategorpDao;
import com.qf.pojo.Categorp;
import com.qf.utils.DruidUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class CategorpDaoImpl implements CategorpDao {
        JdbcTemplate jdbcTemplate=new JdbcTemplate(DruidUtil.getDataSource());


//查询所有信息
    @Override
    public List<Categorp> findAll() {
        String sql="select * from categorp ";
        List<Categorp> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Categorp.class));
        return query;
    }

    //添加商品类别
    @Override
    public int addCategorp(Categorp c) {
        String sql="insert into categorp values(?,?)";
        int update = jdbcTemplate.update(sql, c.getCid(), c.getCname());
        return update;
    }


    //根据cid删除商品类别
    @Override
    public int deleteCategorp(String cid) {
        String sql="delete from categorp where cid=?";
        int update = jdbcTemplate.update(sql, cid);
        return update;
    }

    //根据商品类别编号查询信息
    @Override
    public Categorp findCategorpByCId(String cid) {
        String sql="select * from categorp where cid=?";
        List<Categorp> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Categorp.class),cid);
        if (query.size()!=0 ){
            return query.get(0);
        }
        return null;
    }

    //修改商品类别信息

    @Override
    public int updateCategorpByCId(Categorp c) {
        String sql="update categorp set cname=? where cid=? ";
        int update = jdbcTemplate.update(sql, c.getCname(), c.getCid());
        return update;
    }

}
